import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 *
 *   登录成功后到了主页:
 *
 *   左边的菜单栏的设置:
 *
 *   {
      path: '/',             //跳转的路由
      component: Layout,    // 组件
      redirect: '/dashboard', //重定向到 dashboard
      children: [{            //二级菜单
        path: 'dashboard',    //二级菜单的路由
        name: 'Dashboard',    // 名称
        component: () => import('@/views/dashboard/index'),  //二级菜单对应的组件
        meta: { title: '监控管理平台', icon: 'dashboard' }     //元数据 title 菜单的名称设置  icon图标
      }]
  },
 *
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '监控管理平台', icon: 'dashboard' }
    }]
  },


  {
    path: '/trainer',
    component: Layout,
    redirect: '/trainer/list',
    name: 'Trainer',
    meta: { title: '讲师管理', icon: 'el-icon-s-custom' },
    children: [
      {
        path: 'list',   //路由: /trainer/list
        name: 'List',
        component: () => import('@/views/trainer/index'),
        meta: { title: '查询所有的讲师', icon: 'el-icon-refresh-left' }
      },
      {
        path: 'edit/:id',  //路由: /student/edit
        name: 'Edid',
        component: () => import('@/views/trainer/edit'),
        meta: { title: '讲师编辑', icon: 'el-icon-edit' },
        hidden: true  //隐藏
      },
      {
        path: 'add',  //路由: /trainer/add
        name: 'Add',
        component: () => import('@/views/trainer/add'),
        meta: {
          title: '讲师添加',
          icon: 'el-icon-add'
        },
        hidden: true  //隐藏
      }
    ]
  },
  {
    path: '/course',
    component: Layout,
    redirect: '/course/list',
    name: 'Course',
    meta: { title: '课程管理', icon: 'el-icon-s-custom' },
    children: [
      {
        path: 'list',   //路由: /course/list
        name: 'List',
        component: () => import('@/views/course/index'),
        meta: { title: '查询课程', icon: 'el-icon-refresh-left' }
      },
      {
        path: 'add',  //路由: /course/add
        name: 'Add',
        component: () => import('@/views/course/add'),
        meta: {
          title: '课程添加',
          icon: 'tree'
        }
      },
      {
        path: 'edit/:courseId',  //路由: /course/edit
        name: 'Edit',
        component: () => import('@/views/course/add'),
        meta: {
          title: '课程编辑',
          icon: 'tree'
        },
        hidden: true  //隐藏
      },
      {
        path: 'chapter/:courseId',  //路由: /course/chapter
        name: 'Chapter',
        component: () => import('@/views/course/chapter'),
        meta: {
          title: '课程大纲',
          icon: 'el-icon-add'
        },
        hidden: true  //隐藏
      },
      {
        path: 'publish/:courseId',  //路由: /course/chapter
        name: 'Chapter',
        component: () => import('@/views/course/publish'),
        meta: {
          title: '课程发布',
          icon: 'el-icon-add'
        },
        hidden: true  //隐藏
      }
    ]
  },
  {
    path: '/car',
    component: Layout,
    redirect: '/car/list',
    name: 'Car',
    meta: { title: '汽车之家', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'list',   //路由:/car/list
        name: 'List',
        component: () => import('@/views/car/index'),
        meta: { title: '越野车', icon: 'table' }
      },
      {
        path: 'add',   //路由: /car/add
        name: 'Add',
        component: () => import('@/views/car/add'),
        meta: { title: '添加', icon: 'tree' }
      }
    ]
  },

  {
    path: '/student',
    component: Layout,
    redirect: '/student/list',
    name: 'Student',
    meta: { title: '学生管理', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'list',   //路由: /student/list
        name: 'List',
        component: () => import('@/views/student/index'),
        meta: { title: '查询所有的学生', icon: 'table' }
      },
      {
        path: 'edit',  //路由: /student/edit
        name: 'Edid',
        component: () => import('@/views/student/edit'),
        meta: { title: '学生编辑', icon: 'tree' }
      }
    ]
  },

  {
    path: '/example',
    component: Layout,
    redirect: '/example/table',
    name: 'Example',
    meta: { title: 'Example', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'table',
        name: 'Table',
        component: () => import('@/views/table/index'),
        meta: { title: 'Table', icon: 'table' }
      },
      {
        path: 'tree',
        name: 'Tree',
        component: () => import('@/views/tree/index'),
        meta: { title: 'Tree', icon: 'tree' }
      }
    ]
  },

  {
    path: '/form',
    component: Layout,
    children: [
      {
        path: 'index',
        name: 'Form',
        component: () => import('@/views/form/index'),
        meta: { title: 'Form', icon: 'form' }
      }
    ]
  },

  {
    path: '/nested',
    component: Layout,
    redirect: '/nested/menu1',
    name: 'Nested',
    meta: {
      title: 'Nested',
      icon: 'nested'
    },
    children: [
      {
        path: 'menu1',
        component: () => import('@/views/nested/menu1/index'), // Parent router-view
        name: 'Menu1',
        meta: { title: 'Menu1' },
        children: [
          {
            path: 'menu1-1',
            component: () => import('@/views/nested/menu1/menu1-1'),
            name: 'Menu1-1',
            meta: { title: 'Menu1-1' }
          },
          {
            path: 'menu1-2',
            component: () => import('@/views/nested/menu1/menu1-2'),
            name: 'Menu1-2',
            meta: { title: 'Menu1-2' },
            children: [
              {
                path: 'menu1-2-1',
                component: () => import('@/views/nested/menu1/menu1-2/menu1-2-1'),
                name: 'Menu1-2-1',
                meta: { title: 'Menu1-2-1' }
              },
              {
                path: 'menu1-2-2',
                component: () => import('@/views/nested/menu1/menu1-2/menu1-2-2'),
                name: 'Menu1-2-2',
                meta: { title: 'Menu1-2-2' }
              }
            ]
          },
          {
            path: 'menu1-3',
            component: () => import('@/views/nested/menu1/menu1-3'),
            name: 'Menu1-3',
            meta: { title: 'Menu1-3' }
          }
        ]
      },
      {
        path: 'menu2',
        component: () => import('@/views/nested/menu2/index'),
        name: 'Menu2',
        meta: { title: 'menu2' }
      }
    ]
  },

  {
    path: '/xxx',
    component: Layout,
    children: [
      {
        path: 'https://www.woniuxy.com/',
        meta: { title: '蜗牛学堂', icon: 'link' }
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
